package com.itheima.service.mongo;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //今日佳人的最大缘分值用户
    @Override
    public RecommendUser findMaxScore(Long userId) {

        Query query = new Query(Criteria.where("toUserId").is(userId))
                .with(Sort.by(Sort.Order.desc("score")))
                .with(Sort.by(Sort.Order.desc("date")))
                .skip(0).limit(1);

        return mongoTemplate.findOne(query, RecommendUser.class);
    }




    //根据登录用户id调用service分页查询
    @Override
    public PageBeanVo findRecommendUser(Long userId, Integer pageNum, Integer pageSize) {
        //查询条件
        Query query = new Query(Criteria.where("toUserId").is(userId))
                .with(Sort.by(Sort.Order.desc("score")))
                .with(Sort.by(Sort.Order.desc("date")))
                .skip(((pageNum-1)*pageSize)+1).limit(pageSize);

        //查询
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);

        //统计数据条数
        long count = mongoTemplate.count(query, RecommendUser.class);

        return new PageBeanVo(pageNum,pageSize,count,recommendUserList);
    }


    //2. 调用recommendUserService查询二人缘分值
    @Override
    public RecommendUser findRecommendUser(Long userId, Long recommendUserId) {
        //查询条件
        Query query = new Query(Criteria.where("toUserId").is(userId)
                .and("userId").is(recommendUserId)
        );

        return mongoTemplate.findOne(query, RecommendUser.class);
    }


}
